package com.googlecode.stockhamster.client.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.googlecode.stockhamster.Constant;
import com.googlecode.stockhamster.bank.IBank;
import com.googlecode.stockhamster.utils.XFireStub;

public class BankPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3586563980737866721L;

	private IBank bank;

	private JTextField user;
	private JTextField amount;
	private JLabel bankResult;

	public BankPanel() throws MalformedURLException {
		bank = new XFireStub<IBank>(Constant.bankUrl, IBank.class).getStub();

		add(new JLabel("Username:"));
		user = new JTextField(8);
		add(user);
		add(new JLabel("Amount:"));
		amount = new JTextField(5);
		add(amount);
		JButton hack = new JButton("Hack");
		hack.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (bank.setMoney(Constant.bankAdminCode, user.getText(), Long
						.parseLong(amount.getText())))
					bankResult.setText("Hack: succeed");
				else
					bankResult.setText("Hack: failed");
			}
		});
		add(hack);
		bankResult = new JLabel();
		add(bankResult);
	}
}
